//
//  ST_UpdateUserInfoInputKeyController.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/3.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift


class ST_UpdateUserInfoInputKeyController: ST_UpdateUserInfoChildBaseController {

   
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title_nav_btn.setTitleForAllStates("设置"+self.info_model.summaryTitle)
        
        
        if self.is_display_text_view {
            
            view.addSubview(textView)
            textView.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(20)
                make.height.equalTo(150)
                make.right.equalToSuperview().offset(-23)
                make.top.equalTo(own_navigation_bar.snp.bottom)
            }
        }else{
            view.addSubview(textField)
            textField.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(20)
                make.height.equalTo(65)
                make.right.equalToSuperview().offset(-23)
                make.top.equalTo(own_navigation_bar.snp.bottom).offset(27)
            }
        }
        
       let line1 = UIView()
       line1.backgroundColor = Color.init(hex: 0xF2F4F7)
       self.view.addSubview(line1)
       line1.snp.makeConstraints { (make) in
           make.height.equalTo(1)
           make.right.equalToSuperview()
            if self.is_display_text_view {
            make.left.equalTo(textView)
            make.top.equalTo(textView.snp.bottom)
               
           }else{
               make.left.equalTo(textField)
               make.top.equalTo(textField.snp.bottom).offset(-13)
           }
       }
        
        
        textField.text = info_model.title
        
        textView.text = info_model.title
        
        if self.info_model.summaryTitle == "身高"{
            if info_model.title == "0"{
                textField.text = ""
            }
            textField.keyboardType = .numberPad
        }
        if self.info_model.summaryTitle == "简介"{
            
            self.view.addSubview(placeholderLabel)
            placeholderLabel.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(25)
                make.right.equalToSuperview().offset(-23)
                make.top.equalTo(own_navigation_bar.snp.bottom).offset(8)
            }
            placeholderLabel.isHidden = !info_model.title.isEmpty
        }
        
        
    }
    
    fileprivate lazy var is_display_text_view:Bool = {
        return self.maxCount > 30
    }()
    
    fileprivate lazy var maxCount:Int = {
        if self.info_model.summaryTitle == "简介"{
            return 250  + 10
        }else if self.info_model.summaryTitle == "学校"{
            self.textField.placeholder = "可以写你的学校或者你学习的专业"
            return 15  + 10
        }else if self.info_model.summaryTitle == "职业"{
            self.textField.placeholder = "可以写你的职业或者工作内容"
            return 10  + 10
        }else if self.info_model.summaryTitle == "身高"{
            return 3
        }
        else{
            return 12 + 10
        }
    }()
    
    override func right_click() {
        
        if is_display_text_view{
            
            if let text = textView.text,!text.isEmpty{
                
                info_model.title = text
                
                self.update_value_closure?(info_model)
                
                self.goback_previous_controller()
            }
        }else if let text = textField.text,!text.isEmpty{
            
            info_model.title = text
            
            self.update_value_closure?(info_model)
            
            self.goback_previous_controller()
        }
    }

    lazy var placeholderLabel:UILabel = {
        let control = UILabel()
        control.text = "可以介绍一下自己的兴趣、生活态度、交友宣言..."
        control.textColor = Color.init(hexString: "#999999")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    lazy var textField:UITextField = {
        
        let control = UITextField()
        
        control.placeholder = "请输入你的"+self.info_model.summaryTitle
        
        control.textColor = Color.init(hexString: "#333333")
        
        control.tintColor = Color.init(hexString: "#999999")
        
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        
        control.clearButtonMode = UITextField.ViewMode.whileEditing
     
        control.delegate = self
        
        return control
        
    }()
    
    
    lazy var textView:UITextView = {
        let control = UITextView()
        control.textColor = Color.init(hexString: "#999999")
        control.tintColor = Color.init(hexString: "#999999")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
        control.delegate = self
        
        return control
    }()
    

}
//    MARK: - Delegate
extension ST_UpdateUserInfoInputKeyController:UITextFieldDelegate{

    func textFieldDidEndEditing(_ textField: UITextField) {
        
        if let text = textField.text,!text.isEmpty,text.count>maxCount{
            
            let index = text.index(text.startIndex, offsetBy: maxCount)
            
            let mySubstring = text.prefix(upTo: index)
            
            textField.text  =  String(mySubstring)
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let currentString: NSString = textField.text! as NSString
        let newString: NSString =
            currentString.replacingCharacters(in: range, with: string) as NSString
        return newString.length <= maxCount
    }
    
}


extension ST_UpdateUserInfoInputKeyController:UITextViewDelegate{
    

    func textViewDidChange(_ textView: UITextView) {
        placeholderLabel.isHidden = !textView.text.isEmpty
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        placeholderLabel.isHidden = !textView.text.isEmpty
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        
        if let text = textView.text,!text.isEmpty,text.count>maxCount{
            
            let index = text.index(text.startIndex, offsetBy: maxCount)
            
            let mySubstring = text.prefix(upTo: index)
            
            textView.text  =  String(mySubstring)
        }
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        let currentString: NSString = textView.text! as NSString
        
        let newString: NSString =
            currentString.replacingCharacters(in: range, with: text) as NSString
        
        return newString.length <= maxCount
    }
    
    
}
